package com.yc.cloneDemo;

import com.yc.cloneDemo.deepClone.Role;
import com.yc.cloneDemo.deepClone.User;
import com.yc.cloneDemo.shallowClone.Course;
import com.yc.cloneDemo.shallowClone.Student;

/**
 * Title:克隆测试<br>
 * Description: <br>
 * Author:陈圆(chenyuan@bosssoft.com.cn) <br>
 * Date:2020/8/28
 */
public class CloneDemo {

    public static void main(String[] args) {
//        testStuCour();
//        testRoleUser();

        User u1 = new User();
        u1.setName("zhangsan");
        u1.setNum(1);
        CloneDemo cloneDemo = new CloneDemo();
        cloneDemo.processUser(u1);
        System.out.println(u1.getName());
    }

    public void processUser(User u) {
        u.setName("修改了");
    }

    public static void testRoleUser() {
        User u1 = new User();
        u1.setName("zhangsan");
        u1.setNum(1);
        Role r = new Role();
        r.setRoleName("admin");
        u1.setRole(r);


        User u2 = u1.clone();
        u2.setName("lisi");
        u2.setNum(2);
        Role r2 = u2.getRole();
        final Role clone2 = r2.clone();
        clone2.setRoleName("admin2");
        u2.setRole(clone2);
        System.out.println(u1);
        System.out.println(u2);
    }

    public static void testStuCour(){
        Course course = new Course();
        course.setCourseName("java课程");
        course.setCourseNo(1);  // 定义课程

        Student student1 = new Student();
        student1.setAge(18);
        student1.setStuName("张三");
        student1.setStuNo(1);
        student1.setCourse(course); // 定义学生

        Student student2 = student1.clone();
        student2.setStuNo(2);
        student2.setStuName("张三2");
        student2.setAge(20); // 克隆学生
//        Course course2 = new Course();
        Course course2 = student1.getCourse();
        course2.setCourseNo(2);
        course2.setCourseName("java课程2");
        student2.setCourse(course2); // 修改克隆的课程

        System.out.println(student1);
        System.out.println(student2); // 结果 学生1的课程被修改了，基础数据不变 这就是浅克隆
    }
}
